﻿using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Secretary
{
    public partial class Secretary_SecretaryManagerForm_List : BaseForm
    {
        ServiceSecretary service = new ServiceSecretary();
        public Secretary_SecretaryManagerForm_List()
        {
            InitializeComponent();
            BindComboxNum();
            BindComBox();
            BindDataGrid_CQ();
            BindDataGrid_KS();
        }

        private void BindComBox()
        {
            this.cmbGroup.Properties.Columns.Add(new LookUpColumnInfo("GroupNo", "分组代码", 10));
            this.cmbGroup.Properties.Columns.Add(new LookUpColumnInfo("GroupName", "分组名称"));
            using (DataTable dt = service.GetGroupList(SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamLevel))
            {
                if (dt != null)
                {
                    this.cmbGroup.Properties.DataSource = dt.DefaultView;
                    this.cmbGroup.Properties.ValueMember = "GroupNo";
                    this.cmbGroup.Properties.DisplayMember = "GroupName";
                    if (dt.Rows.Count > 0)
                    {
                        cmbGroup.EditValue = dt.Rows[0]["GroupNo"].ToString().Trim();
                        cmbGroup.Text = dt.Rows[0]["GroupName"].ToString().Trim();
                    }
                }
            }
        }

        private void BindComboxNum()
        {
            this.cmbNum.Properties.NullText = "";
            this.cmbNum.Properties.Columns.Add(new LookUpColumnInfo("Key", "省份代码"));
            this.cmbNum.Properties.Columns.Add(new LookUpColumnInfo("Value", "省份名称"));
            Dictionary<string, string> dicList = new Dictionary<string, string>();
            for (int i = 1; i <= 40; i++)
            {
                dicList.Add(i.ToString(), i.ToString());
            }
            this.cmbNum.Properties.DataSource = dicList.ToList();
            this.cmbNum.Properties.DisplayMember = "Value";
            this.cmbNum.Properties.ValueMember = "Key";
            this.cmbNum.EditValue = "5";
            this.cmbNum.Properties.Columns[0].Visible = false;
            this.cmbNum.Properties.ShowHeader = false;
        }

        private void cmbGroup_EditValueChanged(object sender, EventArgs e)
        {
            BindDataGrid_CQ();
            BindDataGrid_KS();
        }



        private void BindDataGrid_CQ()
        {
            this.gridHKQ.DataSource = null;
            string strGroupNo = string.Empty;
            strGroupNo = this.cmbGroup.EditValue.ToSafeString();
            using (DataTable dt = service.GetScoreQuery_Sec(strGroupNo, SessionerHelper.ExamRoomID))
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataView dv = dt.DefaultView;
                    dv.AllowNew = false;
                    this.gridHKQ.DataSource = dv;
                }
            }
        }

        private void BindDataGrid_KS()
        {
            this.gridKSQ.DataSource = null;
            string strGroupNo = string.Empty;
            strGroupNo = this.cmbGroup.EditValue.ToSafeString();
            using (DataTable dt = service.GetScoreQuery_Sec_KS(strGroupNo, SessionerHelper.ExamRoomID))
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataView dv = dt.DefaultView;
                    dv.AllowNew = false;
                    this.gridKSQ.DataSource = dv;
                }
            }
        }

        private void btn_FP_Click(object sender, EventArgs e)
        {
            this.lbYDF.Text = string.Empty;
            this.lbNoDF.Text = string.Empty;
            if (this.cmbGroup.EditValue != null && this.cmbGroup.EditValue.ToString().Length > 0)
            {
                string strGroupNo = this.cmbGroup.EditValue.ToString();
                using (DataTable dtScoreQuery_Sec_KS = service.GetScoreQuery_Sec_KS(string.Empty, SessionerHelper.ExamRoomID))
                {
                    if (dtScoreQuery_Sec_KS != null && dtScoreQuery_Sec_KS.Rows.Count > 0)
                    {
                        using (DataTable dt = service.GetTeacherNoDF(strGroupNo, SessionerHelper.ExamRoomID))
                        {
                            string strYDF = string.Empty;
                            string strNDF = string.Empty;
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                GetSFDF(dt, ref strYDF, ref strNDF);
                                if (strNDF.Length == 0)
                                {
                                    SaveSecExaming(strGroupNo);
                                }
                                else
                                {
                                    DevHelper.ShowErrorMessageBox("不能进行分配:该考场(" + dtScoreQuery_Sec_KS.Rows[0]["GroupName"].ToSafeString() + ")已经有考生正在考试或者考官还没有全部打完分!");
                                }
                            }
                            else
                            {
                                DevHelper.ShowErrorMessageBox("不能进行分配:该考场(" + dtScoreQuery_Sec_KS.Rows[0]["GroupName"].ToSafeString() + ")已经有考生正在考试!");
                            }
                        }
                    }
                    else
                    {
                        SaveSecExaming(strGroupNo);
                    }
                }
            }
            else
            {
                DevHelper.ShowErrorMessageBox("请先选择分组再进行操作!");
            }
        }

        private static void GetSFDF(DataTable dt, ref string strYDF, ref string strNDF)
        {
            Dictionary<string, string> dicList = new Dictionary<string, string>();
            foreach (DataRow dr in dt.Rows)
            {
                string strUserName = dr["ExaminerName"].ToSafeString();
                string strScoreAll = dr["ScoreAll"].ToSafeString();
                if (dicList.ContainsKey(strUserName))
                {
                    string strScoreAllHis = dicList[strUserName];
                    if (strScoreAll.Length > 0)
                    {
                        if (strScoreAllHis.Length == 0)
                        {
                            dicList.Remove(strUserName);
                            dicList.Add(strUserName, "");
                        }
                    }
                    else
                    {
                        if (strScoreAllHis.Length > 0)
                        {
                            dicList.Remove(strUserName);
                            dicList.Add(strUserName, "");
                        }
                    }
                }
                else
                {
                    dicList.Add(strUserName, strScoreAll);
                }
            }
            foreach (KeyValuePair<string, string> key in dicList)
            {
                if (key.Value.ToSafeString().Length > 0)
                {
                    if (!strYDF.Contains(key.Key.ToSafeString()))
                    {
                        if (strYDF.Length == 0)
                        {
                            strYDF += key.Key.ToSafeString();
                        }
                        else
                        {
                            strYDF += "," + key.Key.ToSafeString();
                        }
                    }
                }
                else
                {
                    if (!strNDF.Contains(key.Key.ToSafeString()))
                    {
                        if (strNDF.Length == 0)
                        {
                            strNDF += key.Key.ToSafeString();
                        }
                        else
                        {
                            strNDF += "," + key.Key.ToSafeString();
                        }
                    }
                }
            }
        }

        private void SaveSecExaming(string strGroupNo)
        {
            if (XtraMessageBox.Show("请确认考官评分是否已打印?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                string strNum = "0";
                if (this.cmbNum.EditValue != null && this.cmbNum.EditValue.ToString().Length > 0)
                {
                    strNum = cmbNum.EditValue.ToSafeString();
                }
                JsonMessage message = service.SaveSecExaming(strNum, strGroupNo, SessionerHelper.ExamRoomID, SessionerHelper.ExamRoomName);
                if (message.State == "true")
                {
                    BindDataGrid_CQ();
                    BindDataGrid_KS();
                    if (gridKSQ.DataSource != null && ((DataView)gridKSQ.DataSource).Count > 0)
                    {
                        DevHelper.ShowErrorMessageBox("考生分配成功,请通知考生考试!");
                    }
                }
                else
                {
                    DevHelper.ShowErrorMessageBox("考生分配出现异常:" + message.Msg);
                }
            }
        }

        private void btn_CheckScore_Click(object sender, EventArgs e)
        {
            string strGroupNo = string.Empty;
            if (this.cmbGroup.EditValue != null && this.cmbGroup.EditValue.ToString().Length > 0)
            {
                strGroupNo = this.cmbGroup.EditValue.ToString();
                using (DataTable dt = service.GetTeacherNoDF(strGroupNo, SessionerHelper.ExamRoomID))
                {
                    string strYDF = string.Empty;
                    string strNDF = string.Empty;
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        GetSFDF(dt, ref strYDF, ref strNDF);
                        this.lbNoDF.Text = strNDF;
                        this.lbYDF.Text = strYDF;
                        if (strNDF.Length == 0)
                        {
                            service.SaveCalculationAvgPoints(strGroupNo, SessionerHelper.ExamRoomID);
                        }
                    }
                    else
                    {
                        DevHelper.ShowErrorMessageBox("没有查询到相对应考官信息!");
                    }
                }
            }
            else
            {
                DevHelper.ShowErrorMessageBox("请先选择分组信息再进行操作!");
            }
        }

        private void btn_PrintScore_Click(object sender, EventArgs e)
        {

        }
    }
}
